﻿#region Copyright (c) 2010-2011 SFJoy .All rights reserved
//******************************************************   
//* Copyright (c) 2010-2011 SFJoy .All rights reserved
//* FileName:		XmlSerializable.cs
//* CLR version: 2.0.50727.3615
//* Author: 			SFJoy
//* Version:		 	1.0.0
//* Date:        2011-2-17 9:59:10"
//* Description:	
//*
//* History:			
//*******************************************************
#endregion Copyright (c) 2010-2011 Yeepay .All rights reserved
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Globalization;

namespace UtilityLibrary.Serialize
{
    public sealed class XmlSerializable
    {
        private XmlSerializable() { }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Serialization(Object value)
        {
            return Serialization(value, "utf-8", value.GetType());
        }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Serialization<T>(Object value)
        {
            return Serialization(value, "utf-8", typeof(T));
        }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string Serialization(Object value, Type type)
        {
            return Serialization(value, "utf-8", type);
        }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Serialization(Object value, String encode)
        {
            return Serialization(value, encode, value.GetType());
        }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Serialization<T>(Object value, String encode)
        {
            return Serialization(value, encode, typeof(T));
        }

        /// <summary>
        /// 对象序列化为xml字符串
        /// </summary>
        /// <param name="value">被序列化的对象</param>
        /// <param name="encode">编码格式字符串</param>
        /// <param name="type">被序列化的类型</param>
        /// <returns></returns>
        public static string Serialization(Object value, String encode, Type type)
        {
            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb, CultureInfo.CurrentCulture);

            XmlSerializer serializer = new XmlSerializer(type);
            serializer.Serialize(writer, value);
            return sb.ToString().Replace("utf-16", encode);
        }

        /// <summary>
        /// 对象反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content">xml</param>
        /// <returns></returns>
        public static object Deserialization(String xml, Type type)
        {
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            XmlSerializer serializer = new XmlSerializer(type);
            return serializer.Deserialize(reader);
        }
    }
}
